//
//  CHDemoViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/21.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHDemoViewController.h"
#import "CHCalendarView.h"
#import "CHItemModel.h"
#import "CHDateModel.h"
#define Kheight 400
@interface CHDemoViewController ()

@property (strong, nonatomic) NSMutableArray *dayModelArray;
@property (nonatomic ,strong) NSMutableArray *listArr;
@property (nonatomic ,strong) NSMutableArray *months;
@property (strong, nonatomic) NSDate *tempDate;
@property (nonatomic ,strong) UIScrollView  *scrollView;
@end

@implementation CHDemoViewController
-(NSMutableArray *)listArr{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(NSMutableArray *)months{
    if (!_months) {
        _months = [NSMutableArray array];
    }
    return _months;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"累计未上传天数";
    self.view.backgroundColor = KRGB(239, 239, 249);
    
    
    [BQActivityView showActiviTy];
    // 避免控制器出栈时，由于数据未加载完成，导致label被强制保留
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //  计算日期
        [weakSelf getDays];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //  更新UI
            [weakSelf updateUI];
            [BQActivityView hideActiviTy];
            
        });
        
    });
    
}


-(void)getDays{
    
    NSMutableArray *listAry = [[NSMutableArray alloc]init];
    for (int i = 0; i <self.datas.count; i++) {
        NSString *str = self.datas[i];
        str = [str substringToIndex:7];
        if (![listAry containsObject:str]) {
            
            [listAry addObject:str];
            NSLog(@"分类%@",listAry);
        }
    }
    
    
    //    获取不同的月份
    for (int i = 0; i < listAry.count; i++) {
        NSDate *date = [self turnDateWithDateStr:listAry[i]];
        [self.listArr addObject:date];
    }
    
    for (int i = 0; i< listAry.count; i++) {
        NSString *str = listAry[i];
        CHDateModel *model = [[CHDateModel alloc] init];
        NSMutableArray *dayArr = [NSMutableArray array];
        for (int j = 0; j < self.datas.count; j++) {
            if ([self.datas[j] hasPrefix:str]) {
                
                [dayArr addObject:self.datas[j]];
            }
        }
        model.dayArr = dayArr;
        [self.months addObject:model];
        
    }
    
}


-(void)updateUI{
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH-64)];
    [self.view addSubview:self.scrollView];
    
    
    for (int i = 0; i < self.months.count; i++) {
        
        CHCalendarView *calendarView = [[CHCalendarView alloc] initWithFrame:CGRectMake(0, i*(Kheight+10), UIScreenW, Kheight)];
        CHDateModel *model = self.months[i];
        calendarView.dateArr = model.dayArr;
        NSLog(@"%@----%@",model.dayArr,self.listArr[i]);
        
        
        [self.scrollView addSubview:calendarView];
        
    }
    
    self.scrollView.contentSize = CGSizeMake(UIScreenW, self.listArr.count * (Kheight+10));
}



-(NSDate *)turnDateWithDateStr:(NSString *)dateStr{
    
    //    NSString *year = [dateStr substringToIndex:5];
    //    NSString *month = [dateStr substringFromIndex:5];
    //    month =  [NSString stringWithFormat:@"%02zd", [month integerValue] +1];
    //    NSString *str = [year stringByAppendingString:month];
    
    // 日期格式化类
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    
    // 设置日期格式 为了转换成功
    
    format.dateFormat =  @"yyyy-MM";
    //  [format setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *date = [format dateFromString:dateStr];
    
    return date;
    
}

#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.datas =  dict[@"time"];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
